package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Notice;
import cn.wolfcode.enums.NoticeLevelEnum;
import cn.wolfcode.qo.NoticeQueryObject;
import cn.wolfcode.service.INoticeService;
import cn.wolfcode.util.RequiredPermission;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private INoticeService noticeService;



    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") NoticeQueryObject qo){
        model.addAttribute("enums", NoticeLevelEnum.values());
        PageInfo<Notice> pageInfo = noticeService.query(qo);
        Employee user = UserContext.getContextUser();
        if(user.getId()!=1){
            List<Notice> list = pageInfo.getList();
            List<Notice> list1 = noticeService.preHandler(list);
            pageInfo.setList(list1);
        }
        model.addAttribute("pageInfo", pageInfo);
        return "notice/list";
    }


    @RequiredPermission(name="公告删除",expression="notice:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           noticeService.delete(id);
        }
        return "redirect:/notice/list";
    }


    @RequiredPermission(name="公告新增/编辑",expression="notice:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Notice notice) {
        if (notice.getId() == null) {
            noticeService.save(notice);
            noticeService.insertRelation(notice.getId());
        } else {
            noticeService.update(notice);
        }
        return "redirect:/notice/list";
    }


    @RequiredPermission(name="公告新增/编辑",expression="notice:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("notice",noticeService.get(id));
        }
        return "notice/input";
    }

    @RequestMapping("/show")
    public String show(Model model,Long id){
        Notice notice = noticeService.get(id);
        Long eid = UserContext.getContextUser().getId();
        noticeService.hasRead(id, eid);  //更改未读已读状态
        UserContext.setUnreadnumInSession(noticeService.getUnreadNum(eid)); //更改session中未读消息数量
        UserContext.setUnreadInSession(noticeService.getUnread(eid));  //更改session中未读消息list
        model.addAttribute("notice",notice);

        return "notice/show";
    }
//    @RequestMapping("/hasRead")
//    @ResponseBody
//    public String hasRead(Long id){
//        noticeService.hasRead(id,UserContext.getContextUser().getId());
//        return null;
//    }

    @RequestMapping("/updateStatus")
    @ResponseBody
    public String updateStatus(Boolean status,Long id){
        noticeService.updateStatus(status,id);
        Long eid = UserContext.getContextUser().getId();
        UserContext.setUnreadnumInSession(noticeService.getUnreadNum(eid)); //更改session中未读消息数量
        UserContext.setUnreadInSession(noticeService.getUnread(eid));
        if(!status){
            noticeService.restReadStatus(id);
        }
        return null;
    }
}
